package com.easymin.weixin.controller.v4;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.SimpleCarPoolManager;
import com.easymin.weixin.manager.WxApplicationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController("simpleCarPoolV4Res")
@RequestMapping("api/simpleCarPool/v4")
@SuppressWarnings("unchecked")
public class SimpleCarPoolV4Res {
  @Autowired
  private SimpleCarPoolManager simpleCarPoolManager;
  @Autowired
  private WxApplicationManager wxApplicationManager;
  @Autowired
  private DaijiaApi daijiaApi;

  /**
   * 工作车下单.
   * @param roadLineId 路线Id
   * @param bookLat 上车地点纬度
   * @param bookLng 上车地点经度
   * @param bookTime 上车时间
   * @param bookArea 上车地点
   * @param appKey 区分系统
   */
  @RequestMapping(value = {"book"}, method = RequestMethod.POST)
  public ResultTransport book(
      @RequestParam("roadLineId") Long roadLineId,
      @RequestParam("bookLat") Double bookLat, 
      @RequestParam("bookLng") Double bookLng,
      @RequestParam("bookTime") Long bookTime, 
      @RequestParam("bookArea") String bookArea,
      @RequestParam("employToken") String employToken, 
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    ResultTransport employResult =
        daijiaApi.findDriverV4Token(wxApplication.getAddress(), employToken);
    if (employResult.getCode() != 0) {
      return employResult;
    }

    Map<String, Object> emap = (Map<String, Object>) employResult.getData();

    Long employId = Long.valueOf(emap.get("employId").toString());
    String employRealName = (String) emap.get("employRealName");
    String employPhone = (String) emap.get("employPhone");

    ResultTransport instanceResult = new ResultTransport();

    instanceResult =
        simpleCarPoolManager.bookV4(roadLineId, bookLat, bookLng, bookTime, bookArea, employId,
            employRealName, employPhone, appKey);

    if (instanceResult.getCode() == 0) {
      Map<String, Object> map = (Map<String, Object>) instanceResult.getData();
      String drivers = map.get("workCarDrivers").toString();

      daijiaApi.simplecarpoolV4(wxApplication.getAddress(), drivers, employId);
    }
    return instanceResult;
  }

  /**
   * 查询服务人员附近线路.
   * @param lat 服务人员当前地址纬度
   * @param lng 服务人员当前地址经度
   * @param appKey 区分系统
   * @return 多条路线
   */
  @RequestMapping(value = {"roadLines"}, method = RequestMethod.GET)
  public ResultTransport roadLines(
      @RequestParam("lat") Double lat,
      @RequestParam("lng") Double lng,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = simpleCarPoolManager.roadLinesV4(lat, lng, appKey);
    return resultTransport;
  }

  /**
   * 查询指定线路.
   * @return 一条工作车路线
   */
  @RequestMapping(value = {"roadLine/{roadLineId}"}, method = RequestMethod.GET)
  public ResultTransport roadLine(
      @PathVariable("roadLineId") Long roadLineId,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = simpleCarPoolManager.roadLineV4(roadLineId, appKey);
    return resultTransport;
  }

  /**
   * 查询车辆位置.
   * @param carId 车辆Id
   * @param appKey 区分系统
   * @return 一辆工作车
   */
  @RequestMapping(value = {"car/{carId}"}, method = RequestMethod.GET)
  public ResultTransport car(
      @PathVariable("carId") Long carId,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = simpleCarPoolManager.carV4(carId, appKey);

    if (resultTransport.getCode() == 0) {
      Long driverId = null;
      if (resultTransport.getData() != null) {
        driverId = Long.valueOf(resultTransport.getData().toString());
      }
      if (null == driverId) {
        return resultTransport;
      }

      WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
      ResultTransport transport = daijiaApi.findDriverV4(wxApplication.getAddress(), driverId);
      if (transport.getCode() == 0) {
        Map<String, Object> emap = (Map<String, Object>) transport.getData();
        String address = emap.get("employAddress").toString();
        Double employLat = Double.valueOf(emap.get("employLat").toString());
        Double employLng = Double.valueOf(emap.get("employLng").toString());
        Map<String, Object> data = new HashMap<String, Object>();

        data.put("address", address);
        data.put("lat", employLat);
        data.put("lng", employLng);

        transport.setData(data);
        return transport;
      } else {
        return transport;
      }
    } else {
      return resultTransport;
    }
  }

  /**
   * V4根据员工Id(被接的人)查询工作车订单.
   */
  @RequestMapping(value = {"books/{employId}"}, method = RequestMethod.GET)
  public ResultTransport books(
      @PathVariable("employId") Long employId,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = simpleCarPoolManager.booksV4(employId, appKey);
    return resultTransport;
  }

  /**
   * (被接的人)取消订单.
   * @param bookId 工作车订单Id
   * @param appKey 区分系统
   */
  @RequestMapping(value = {"cancel/{bookId}"}, method = RequestMethod.POST)
  public ResultTransport cancel(
      @PathVariable("bookId") Long bookId,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = simpleCarPoolManager.cancelV4(bookId, appKey);
    if (resultTransport.getCode() == 0) {
      WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
      Map<String, String> map = (Map<String, String>) resultTransport.getData();
      String employName = map.get("employName").toString();
      String drivers = map.get("drivers").toString();

      resultTransport =
          daijiaApi.simplecarpoolCancelV4(wxApplication.getAddress(), drivers, employName);

      return resultTransport;
    } else {
      return resultTransport;
    }
  }
}
